<template>
	<view>
		<u-navbar title-color="#ffffff" title="合伙人专区" back-text="返回" back-icon-color="#ffffff" :back-text-style="textObj"
		 :border-bottom="false" :background="background"></u-navbar>
		<view class="main-mask">
			<view class="mask-box"></view>
		</view>
		<view class="main">
			<view class="box">
				<view class="">余额</view>
				<view class="">{{currentPartnerFee}}</view>
			</view>
			<view class="box">
				<view class="">待分金额</view>
				<view class="">{{waitProceedPartnerFee}}</view>
				<view class="line"></view>
			</view>
			<view class="btn" @click="approveStatus ? $u.route('/pages/user/withdrawal/withdrawal',{
			currentEnableFee:currentPartnerFee,
			typeIndex:3
		}) : approvePopup = true">
				<view>提现</view>
			</view>
		</view>
		<view class="container">
			<view class="content">
				<view class="title">
					<view class="left">
						<view @click="typeIndex = index" v-for="(item,index) in type" :key="index" class="active">{{item}}
							<!-- <view class="icon" v-if="index == typeIndex">
								
							</view> -->
						</view>
					</view>
					<view class="right" @click="show = true">
						<u-image :src="appletImgUrl+'screen-icon.png'" width="38" height="38"></u-image>
						<!-- <text>{{currentDate}}</text> -->
						<text>筛选</text>
						<!-- <u-icon size="18" name="arrow-down-fill" color="#E1E0E0"></u-icon> -->
					</view>
				</view>
				<view class="list">
					<view class="loading" v-if="list == '' && !fail">
						<u-loading mode="circle"></u-loading>
					</view>
					<view v-if="fail" style="padding-top: 300rpx;">
						<u-empty mode="list"></u-empty>
					</view>
					<view class="choice-date" v-if="showTip && list != ''">筛选时间：{{firstDatetime}}至{{lastDatetime}}
						<u-icon name="close" @click="showTip = false"></u-icon>
					</view>
					<view class="item" v-for="(item,index) in list" :key="index" :style="{
						'border-top':typeIndex == 1 && index != 0 ? '1rpx solid #E1E0E0':'none'
					}">
						<view class="other" v-if="typeIndex == 1">
							<view class="">{{item.new_date}}</view>
							<view class="">共计：
								<text v-if="typeIndex == 1">{{item.dayTotalAmount}}</text>
							</view>
						</view>
						<block v-if="typeIndex == 1">
							<view class="item-list" v-for="(items,indexs) in item.items" :key="indexs">
								<view>
									<view class="">
										{{items.rebateTypeName}}
									</view>
									<view class="">{{items.addTime}}</view>
								</view>
								<view class="">
									<view class="">{{items.statusName}}</view>
									<view class="">{{items.amount}}</view>
								</view>
							</view>
						</block>
						<block v-else>
							<!-- <view class="item-list" v-for="(items,indexs) in item.orderRebateList" :key="indexs">
								<view class="user">
									<u-image width="38rpx" height="38rpx" shape="circle" :src="items.joinUserHeadPhoto ? imageURL + items.joinUserHeadPhoto : '/static/logo.png'"></u-image>
									<view class="name">{{items.joinUserNickName}}</view>
								</view>
								<view>
									<view class="">
										订单号：{{items.orderNumber}}
									</view>
									<view class="">{{items.typeName}}</view>
								</view>
								<view class="">
									<view class="">{{items.addTime}}</view>
									<view class="">{{items.amount}}</view>
								</view>
							</view> -->
							<view class="item-list" style="margin: 0;" @click="$u.route('/pages/user/myAssetDetail/myAssetDetail',{
								integralType:item.integralType,
								startTime:firstDatetime,
								endTime:lastDatetime
							})">
								<view class="">
									<view class="" style="width: 50%;">{{item.integralTypeName}}</view>
									<view class="" style="width: 20%;text-align: center;">{{item.totalCounts}}笔</view>
									<view class="" style="width: 30%;text-align: right;">{{item.totalAmount}}
										<u-icon name="arrow-right" color="#AAAAAA"></u-icon>
									</view>
								</view>
							</view>
						</block>
					</view>

					<u-loadmore :load-text="{loading:'正在加载',loadmore:'点击加载更多',nomore:'没有更多了'}" v-if="list != ''" :status="status"
					 @loadmore="loadmore"></u-loadmore>
					<!-- <u-gap></u-gap> -->
					<!-- <view class="safe-area-inset-bottom"></view> -->
				</view>
			</view>
		</view>

		<!-- <view class="btn" @click="approveStatus ? $u.route('/pages/user/withdrawal/withdrawal',{
			currentEnableFee:currentEnableFee,
			typeIndex:0
		}) : approvePopup = true">
			<view class="btn-box">
				提现
			</view>
			<view class="safe-area-inset-bottom"></view>
		</view> -->
		<u-picker :default-time="defaultTime" mode="time" confirm-color="#FD5C1D" @confirm="confirm" v-model="show" :params="params"></u-picker>
		<u-modal confirm-text="去认证" show-cancel-button confirm-color="#FD5C1D" v-model="approvePopup" @confirm="$u.route('/pages/user/realName/realName',{
			realState: realState
		})"
		 content="您得先实名认证了才可以提现"></u-modal>
	</view>
</template>

<script>
import {appletImgUrl} from "@/common/config.js"
	let uTabOffsetTop = 44 + uni.getSystemInfoSync().statusBarHeight
	export default {
		data() {
			return {
				appletImgUrl:appletImgUrl,
				defaultTime:'',
				showTip: true,
				current: 0,
				approveStatus: false,
				type: ['收入明细'],
				realState: '',
				typeIndex: 1,
				imageURL: this.imageURL,
				status: 'loading',
				approvePopup: false,
				waitProceedPartnerFee: 0,
				params: {
					year: true,
					month: true
				},
				show: false,
				currentPartnerFee: 0,
				currentMallFee: 0,
				// list: [{
				// 	name: '线上'
				// }, {
				// 	name: '线下'
				// }],
				textObj: {
					color: '#ffffff'
				},
				list: [],
				fail: false,
				currentDate: '',
				firstDatetime: '',
				lastDatetime: '',
				background: {
					backgroundImage: 'linear-gradient(90deg, rgba(255, 77, 70, 1), rgba(255, 162, 0, 1))'
				}
			};
		},
		onLoad() {
			this.pageNo = 1
			this.$u.get('userwallet/info').then(res => {
				this.currentPartnerFee = res.currentPartnerFee
				this.waitProceedPartnerFee = res.waitProceedPartnerFee
				// this.currentMallFee = res.currentMallFee
			})


			this.$u.get('userverify/ok').then(res => {
				switch (res.approveStatus) {
					case 20:
						this.approveStatus = true
						break
					case 10:
						this.realState = 1
						break
				}
				if (!res.approveStatus) {
					this.realState = 0
				}
			})

			var date = new Date();
			var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
			var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
			let c = new Date(date)
			let cdate = c.getFullYear() + '-' + (c.getMonth() + 1)
			this.initCurrentDate = cdate
			this.currentDate = cdate

			var f = new Date(firstDay);
			let fMonth = f.getMonth() + 1
			fMonth < 10 ? '0' + fMonth : fMonth
			let fday = f.getDate()
			fday = fday < 10 ? '0' + fday : fday
			var firstDatetime = f.getFullYear() + '-' + fMonth + '-' + fday;

			var l = new Date(lastDay);
			let lMonth = l.getMonth() + 1
			lMonth = lMonth < 10 ? '0' + lMonth : lMonth
			let lday = l.getDate()
			lday = lday < 10 ? '0' + lday : lday

			var lastDatetime = l.getFullYear() + '-' + lMonth + '-' + lday;
			this.initFirstDateTime = firstDatetime
			this.initLastDatetime = lastDatetime

			this.firstDatetime = firstDatetime
			this.lastDatetime = lastDatetime
			this.getList(firstDatetime, lastDatetime, res => {
				// this.status = res.length < 10 ? 'nomore' : 'loadmore'
				this.status = 'loadmore'
			}, () => {
				this.fail = true
			})
		},
		onPageScroll({
			scrollTop
		}) {
			var opacity = 0,
				maxNumber = uTabOffsetTop
			if (scrollTop <= maxNumber) {
				opacity = scrollTop / maxNumber
			} else {
				opacity = 1
			}
			if (opacity == this.opacity) return
			this.background.backgroundColor = `rgba(254,70,102,${opacity})`
		},

		watch: {
			typeIndex(nval, oval) {
				if (this.typeIndex == nval) {
					this.pageNo = 1
					this.fail = false
					this.list = []
					this.status = 'loading'
					if (this.typeIndex == 1) {
						// this.status = 'loadmore'
						if (this.firstFirstIndexDate && this.firstLastIndexDate) {
							this.firstDatetime = this.firstFirstIndexDate
							this.lastDatetime = this.firstLastIndexDate
							this.currentDate = this.firstCurrentDate
						} else {
							this.firstDatetime = this.initFirstDateTime
							this.lastDatetime = this.initLastDatetime
							this.currentDate = this.initCurrentDate
						}
					} else {
						if (this.lastFirstIndexDate && this.lastLastIndexDate) {
							this.firstDatetime = this.lastFirstIndexDate
							this.lastDatetime = this.lastLastIndexDate
							this.currentDate = this.lastCurrentDate
						} else {
							this.firstDatetime = this.initFirstDateTime
							this.currentDate = this.initCurrentDate
							this.lastDatetime = this.initLastDatetime
						}
					}
					this.showTip = true
					// this.nextDay = null
					this.isLoadmore = false
					this.getList(this.firstDatetime, this.lastDatetime, res => {
						if (res == '') {
							this.fail = true
							return
						}
						if (this.typeIndex == 0) {
							this.status = 'nomore'
						} else {
							this.status = 'loadmore'
						}
					})
				}
			}
		},
		// onReachBottom() {
		// 	if (this.status == 'loading' || this.status == 'nomore' || this.typeIndex == 0) return
		// 	this.pageNo++
		// 	this.status = 'loading'
		// 	this.getList(this.firstDatetime, this.lastDatetime, res => {
		// 		if (res == '') {
		// 			this.status = 'nomore'
		// 			return
		// 		}
		// 		this.status = 'loadmore'
		// 	})
		// },
		methods: {
			getNextDate(date, day) {
				var dd = new Date(date);
				dd.setDate(dd.getDate() + day);
				var y = dd.getFullYear();
				var m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
				var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
				return y + "/" + m + "/" + d;
			},
			loadmore() {
				if (this.status == 'loading' || this.status == 'nomore') return
				this.nextDay = this.getNextDate(this.nextDay, -1) + ' 00:00:00'
				this.status = 'loading'
				this.isLoadmore = true
				this.getList(this.firstDatetime, this.lastDatetime, res => {
					if (res == '') {
						this.status = 'nomore'
						return
					}
					this.status = 'loadmore'
					this.list = [...this.list, ...res]
				})
			},
			getList(startTime, endTime, fn = () => {}, fail = () => {}) {
				let filterStr = this.nextDay || null
				if (this.nextDay) {
					filterStr = this.nextDay.replace(/\//g, '-')
				}

				this.$u.post('partnerrebateorderinfo/getPartnerRebateInfo', {
					startTime: startTime + ' 00:00:00',
					nextDay: filterStr
				}).then(res => {
					
					if(Object.keys(res).length != 0){
						let date = res.date
						date = date.replace(/-/g, '/');
						let filterDate = new Date(date)
						var y = filterDate.getFullYear();
						var m = filterDate.getMonth() + 1 < 10 ? "0" + (filterDate.getMonth() + 1) : filterDate.getMonth() + 1;
						var d = filterDate.getDate() < 10 ? "0" + filterDate.getDate() : filterDate.getDate();
						res.new_date = `${y}-${m}-${d}`
					}
					fn(Object.keys(res).length == 0 ? [] : [res])
					if (!this.isLoadmore) {
						if (Object.keys(res).length == 0) {
							this.fail = true
							return
						}
						this.list = [res]
						let date = res.date
						date = date.replace(/-/g, '/');
						this.nextDay = date
					}
				}).catch(error => {
					fail()
					this.status = 'loadmore'
				})
				// if (this.typeIndex == 1) {
				// 	this.$u.post('partnerrebateorderinfo/getPartnerRebateInfo', {
				// 		startTime: startTime + ' 00:00:00',
				// 		nextDay: this.nextDay
				// 	}).then(res => {
				// 		fn(Object.keys(res).length == 0 ? [] : [res])
				// 		if (!this.isLoadmore) {
				// 			if (Object.keys(res).length == 0) {
				// 				this.fail = true
				// 				return
				// 			}
				// 			this.list = [res]
				// 			this.nextDay = res.date
				// 		}
				// 		console.log(this.isLoadmore);
				// 	}).catch(error => {
				// 		this.fail = true
				// 	})
				// } else {
				// 	this.$u.post('cashintegrallog/summary', {
				// 		endTime: endTime,
				// 		startTime: startTime,
				// 		integralTypes: [14]
				// 	}).then(res => {
				// 		if (res.details == '') {
				// 			this.fail = true
				// 			fn([])
				// 			return
				// 		}
				// 		fn(res.details)
				// 		this.list = this.list.concat(res.details)
				// 	}).catch(error => {
				// 		this.fail = true
				// 	})
				// }
			},
			confirm(e) {
				var firstDay = new Date(e.year, e.month, 1);
				var lastDay = new Date(e.year, e.month, 0);
				var f = new Date(firstDay);
				let year = e.month == '12' ? f.getFullYear() - 1 : f.getFullYear()

				let month = e.month == '12' ? 12 : f.getMonth()
				month = month < 10 ? '0' + month : month
				let fday = f.getDate()
				fday = fday < 10 ? '0' + fday : fday

				var firstDatetime = year + '-' + month + '-' + fday;

				var l = new Date(lastDay);

				let lMonth = l.getMonth() + 1
				lMonth = lMonth < 10 ? '0' + lMonth : lMonth
				let lday = l.getDate()
				lday = lday < 10 ? '0' + lday : lday

				var lastDatetime = l.getFullYear() + '-' + lMonth + '-' + lday;
				this.currentDate = `${e.year}-${e.month}`
				// this.defaultTime = this.currentDate
				this.defaultTime = this.currentDate + '-01 00:00:00'
				this.firstDatetime = firstDatetime
				this.lastDatetime = lastDatetime
				this.nextDay = this.lastDatetime + ' 00:00:00'
				this.pageNo = 1
				this.fail = false
				this.list = []
				this.status = 'loading'
				this.isLoadmore = false
				if (this.typeIndex == 1) {
					this.firstFirstIndexDate = firstDatetime
					this.firstLastIndexDate = lastDatetime
					this.firstCurrentDate = `${e.year}-${e.month}`
				} else {
					this.lastFirstIndexDate = firstDatetime
					this.lastLastIndexDate = lastDatetime
					this.lastCurrentDate = `${e.year}-${e.month}`
				}
				this.getList(firstDatetime, lastDatetime, res => {
					if (this.typeIndex == 0) {
						this.status = 'nomore'
					} else {
						this.status = 'loadmore'
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	// .btn {
	// 	position: fixed;
	// 	width: 100%;
	// 	bottom: 0;
	// 	z-index: 2;
	// 	left: 0;
	// 	background: linear-gradient(90deg, #FD5C1D, #FD9B1D);
	// 	border-radius: 40rpx 40rpx 0 0;
	// 	.btn-box {
	// 		line-height: 100rpx;
	// 		color: #FFFFFF;
	// 		text-align: center;
	// 		font-size: 28rpx;
	// 	}
	// }

	.container {
		position: relative;
		z-index: 1;
		top: -35rpx;

		.content {
			border-radius: 40rpx 40rpx 0px 0px;
			background-color: #FFFFFF;
			padding: 0 30rpx;

			.choice-date {
				color: #AAAAAA;
				font-size: 26rpx;
				padding-top: 34rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
			}

			.list {
				// padding-bottom: 65rpx;
				min-height: 50vh;

				.loading {
					display: flex;
					justify-content: center;
					padding-top: 300rpx;
				}

				.item {
					border-top: 1rpx solid #E1E0E0;
					padding: 30rpx 0;

					&:first-child {
						border-top: none;
					}

					.item-list {
						margin-top: 30rpx;

						>view {
							display: flex;
							margin-top: 5rpx;
							align-items: center;
							justify-content: space-between;
						}

						.user {
							justify-content: flex-start;
							margin-bottom: 25rpx;

							.name {
								margin-left: 15rpx;
							}
						}
					}

					.other {
						display: flex;
						align-items: center;
						justify-content: space-between;

						view {
							&:first-child {
								font-weight: bold;
								font-size: 28rpx;
							}

							&:last-child {
								text {
									color: #FF4D46;
									font-size: 48rpx;
									font-weight: bold;
								}
							}
						}
					}
				}
			}

			// .money {
			// 	view {
			// 		&:first-child {
			// 			font-size: 24rpx;
			// 		}

			// 		&:last-child {
			// 			font-weight: bold;
			// 			font-size: 30rpx;

			// 			text {
			// 				font-size: 48rpx;
			// 			}
			// 		}
			// 	}
			// }

			.tab {
				height: 82rpx;
				border-bottom: 1rpx solid #E1E0E0;
			}

			.title {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 45rpx 0;
				border-bottom: 1rpx solid #E1E0E0;

				.right {
					&:last-child {
						// padding: 5rpx 15rpx;
						display: flex;
						// border: 1rpx solid #E1E0E0;
						// border-radius: 4rpx;
						align-items: center;

						text {
							margin-left: 10rpx;
							color: #AAAAAA;
							font-size: 26rpx;
						}
					}

					&:first-child {
						font-weight: bold;
						font-size: 32rpx;
					}
				}

				.left {
					display: flex;
					align-items: center;

					>view {
						color: #AAAAAA;
						position: relative;

						.icon {
							position: absolute;
							bottom: -45rpx;
							left: 50%;
							transform: translateX(-50%);
							width: 44rpx;
							height: 6rpx;
							background: linear-gradient(90deg, #FF7200, #FECD62);
							border-radius: 3rpx;
						}

						&.active {
							font-size: 36rpx;
							color: #000000;
							font-weight: bold;
						}

						&:last-child {
							// margin-left: 40rpx;
						}
					}
				}
			}
		}
	}

	.main-mask {
		background: linear-gradient(90deg, #FF4D46, #FFA200);
		width: 100%;
		position: fixed;
		top: 0;
		left: 0;
		height: 50%;
	}

	.main {
		display: flex;
		position: relative;
		padding-bottom: 70rpx;

		.btn {
			margin-top: 20rpx;
			display: flex;
			justify-content: center;
			position: absolute;
			bottom: 60rpx;
			left: 50%;
			transform: translateX(-50%);

			view {
				border: 1rpx solid #fff;
				border-radius: 10rpx;
				color: #fff;
				// width: 140rpx;
				line-height: 54rpx;
				padding: 0 45rpx;
			}
		}

		.box {
			text-align: center;
			padding-bottom: 75rpx;
			padding-top: 30rpx;
			flex: 1;
			position: relative;



			.line {
				position: absolute;
				top: 50%;
				left: 0;
				transform: translateY(-50%);
				width: 1rpx;
				height: 65rpx;
				background-color: rgba($color: #ffffff, $alpha: .5);
				margin-top: -20rpx;
			}

			view {
				color: #FFFFFF;

				&:first-child {
					font-size: 24rpx;
				}

				&:nth-child(2) {
					font-size: 60rpx;
					font-weight: bold;
					// margin: 10rpx 0;
					margin-top: 5rpx;
					// margin-bottom: 15rpx;
				}

				// &:last-child {
				// 	display: flex;
				// 	justify-content: center;

				// 	navigator {
				// 		font-size: 24rpx;
				// 		border: 1rpx solid #FFFFFF;
				// 		color: #FFFFFF;
				// 		line-height: 56rpx;
				// 		border-radius: 10rpx;
				// 		// width: 170rpx;
				// 		padding: 0 60rpx;
				// 	}
				// }
			}
		}
	}
</style>
